package hlhj.fhp.supreme.fragment

import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.lzy.okgo.OkGo
import com.lzy.okgo.model.Response
import hlhj.fhp.supreme.R
import hlhj.fhp.supreme.activitys.ArticleAty
import hlhj.fhp.supreme.adapters.JiePanListAdp
import hlhj.fhp.supreme.customview.JiePanPayDialog
import hlhj.fhp.supreme.javabean.ArticleBean
import hlhj.fhp.supreme.javabean.ArticleListBean
import hlhj.fhp.supreme.network.Urls
import hlhj.fhp.supreme.toast
import hlhj.fhp.supreme.utils.JsonCallBack
import kotlinx.android.synthetic.main.rcylist.view.*

/**
 * Created by Never Fear   on 2018\7\17 0017.
Never More....
 */
class JiePanListFgm :Fragment() {
    companion object {
        fun getInstence(id:Int,name:String): JiePanListFgm {
            val jiePanListFgm = JiePanListFgm()
            val bundle = Bundle()
            bundle.putInt("id",id)
            bundle.putString("name",name)
            jiePanListFgm.arguments=bundle
            return jiePanListFgm
        }
    }
    private lateinit var rootView:View
    private lateinit var adp:JiePanListAdp
    private val datas=ArrayList<ArticleListBean.DataBean>()
    override fun onCreateView(inflater: LayoutInflater?, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater?.inflate(R.layout.rcylist,null)!!

        adp= JiePanListAdp(datas)
        rootView.recyclerview.adapter=adp
        rootView.recyclerview.layoutManager=LinearLayoutManager(context,LinearLayoutManager.VERTICAL,false)


        despatchData()
        initListener()
        return rootView
    }

    private fun initListener() {
        adp.setOnItemClickListener { adapter, view, position ->
            OkGo.post<ArticleBean>(Urls.BaseUrl+Urls.ARTICLE_DETAIL)
                    .params("id",datas[position].id)
                    .execute(object :JsonCallBack<ArticleBean>(activity){
                        override fun onSuccess(response: Response<ArticleBean>?) {
                            val articleBean = response?.body()
                            if (articleBean?.code==1){
                                val intent = Intent(context, ArticleAty::class.java)
                                intent.putExtra("id",datas[position].id)
                                intent.putExtra("rid",datas[position].room_id.toString())
                                context!!.startActivity(intent)
                            }else if (articleBean?.code==403){
                                JiePanPayDialog(activity,"111").show()
                            }
                            else{
                                this@JiePanListFgm.toast(context,articleBean?.msg!!)
                            }
                        }
                    })
        }
    }

    private fun despatchData() {
        OkGo.post<ArticleListBean>(Urls.BaseUrl+Urls.JIEPAN_ARTICLE)
                .params("cat_id",arguments.getInt("id"))
                .execute(object :JsonCallBack<ArticleListBean>(activity){
                    override fun onSuccess(response: Response<ArticleListBean>?) {
                        val listBean = response?.body()!!
                        if (listBean.code==1){
                            if (listBean.data!=null){
                                datas.addAll(listBean.data)
                                adp.notifyDataSetChanged()
                            }
                        }else{
                            this@JiePanListFgm.toast(listBean.msg)
                        }
                    }
                })
    }
}